
package net.swa.dewei.web.action;

import java.io.BufferedOutputStream;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.vo.Statis;
import net.swa.dewei.beans.vo.StatisMendian;
import net.swa.dewei.beans.vo.StatisPlusMendian;
import net.swa.dewei.service.StatisService;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.DateUtils;
import net.swa.util.JsonResult;

/**电池信息表 Action**/
public class StatisAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -9167064623097370397L;

    private final Logger log = Logger.getLogger(StatisAction.class);

    /** 公用方法接口 */
    private ICommonService commonService;

    private StatisService statisService;

    private String sdate;

    private String edate;

    private String name;

    private String changeType;

    private Long fuwuZhanId;

    /**初始化分页查询**/
    @Log(description = "进入统计分页查询")
    public String listPage()
    {
        sdate = DateUtils.getfirstDayOfThisMonth();
        edate = DateUtils.getCurrDate("yyyy-MM-dd");
        return "listPage";
    }

    public String queryPage() throws Exception
    {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 10 : pageSize;
        JsonResult<Statis> json = null;

        log.debug("查询统计结果数据。。。");
        json = statisService.queryPage(sdate, edate, name, currentPage, pageSize);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }

    /**
     * 根据条件导出excel
     * @return
     * @throws IOException 
     */
    public String exportExcel() throws IOException
    {
        HSSFWorkbook workbook = statisService.exportExcel(sdate, edate, name);
        HttpServletResponse response = getResponse();
        BufferedOutputStream bos = null;
        try
        {
            response.setContentType("application/vnd.ms-excel");
            String dateStr = DateUtils.getCurrDate("yyyyMMdd");
            String filename = new String(dateStr.getBytes("gbk"), "iso-8859-1") + ".xls";
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            bos = new BufferedOutputStream(response.getOutputStream());
            workbook.write(bos);
            bos.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            bos.close();
        }
        return null;
    }

    /**初始化分页查询**/
    @Log(description = "进入统计分页查询")
    public String listFuwuzhan()
    {
        sdate = DateUtils.getfirstDayOfThisMonth();
        edate = DateUtils.getCurrDate("yyyy-MM-dd");
        User user = getCurrentUser();
        if (null != user)
        {
            if (SysConst.USER_TYPE_FUWUZHAN == user.getUserType())
            {
                fuwuZhanId = user.getFuwuzhanId();
                return "listFuwuzhan";
            }
        }
        return "notice";
    }

    public String queryPageByFwz() throws Exception
    {
        pageSize = pageSize == 0 ? 10 : pageSize;
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 10 : pageSize;
        JsonResult<StatisMendian> json = null;
        log.debug("查询统计结果数据。。。");
        json = statisService.queryStaticPage(fuwuZhanId, sdate, edate, name, changeType, currentPage, pageSize);
        outJson(json);
        return null;
    }

    
    public String exportExcelFwz() throws IOException
    {
        HSSFWorkbook workbook = statisService.exportExcelFwz(fuwuZhanId, sdate, edate, name, changeType);
        HttpServletResponse response = getResponse();
        BufferedOutputStream bos = null;
        try
        {
            response.setContentType("application/vnd.ms-excel");
            String dateStr = DateUtils.getCurrDate("yyyyMMdd");
            String filename = new String(dateStr.getBytes("gbk"), "iso-8859-1") + ".xls";
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            bos = new BufferedOutputStream(response.getOutputStream());
            workbook.write(bos);
            bos.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            bos.close();
        }
        return null;
    }
    
    
    /**初始化分页查询**/
    @Log(description = "进入门店新增统计分页查询")
    public String listPlusMend()
    {
        sdate = DateUtils.getfirstDayOfThisMonth();
        edate = DateUtils.getCurrDate("yyyy-MM-dd");
        return "listPlusMend";
    }
    
    public String queryPlusMendPage() throws Exception
    {
        pageSize = pageSize == 0 ? 10 : pageSize;
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 10 : pageSize;
        JsonResult<StatisPlusMendian> json = null;
        log.debug("查询统计结果数据。。。");
        json = statisService.queryPlusMendPage(fuwuZhanId, sdate, edate, name, orderBy,orderType, currentPage, pageSize);
        outJson(json);
        return null;
    }
    
    
    public ICommonService getCommonService()
    {
        return commonService;
    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    @Required
    @Resource
    public void setStatisService(StatisService statisService)
    {
        this.statisService = statisService;
    }

    public String getSdate()
    {
        return sdate;
    }

    public void setSdate(String sdate)
    {
        this.sdate = sdate;
    }

    public String getEdate()
    {
        return edate;
    }

    public void setEdate(String edate)
    {
        this.edate = edate;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getChangeType()
    {
        return changeType;
    }

    public void setChangeType(String changeType)
    {
        this.changeType = changeType;
    }

    public Long getFuwuZhanId()
    {
        return fuwuZhanId;
    }

    public void setFuwuZhanId(Long fuwuZhanId)
    {
        this.fuwuZhanId = fuwuZhanId;
    }

}
